﻿using System;
using System.Data;

using Model.Data;
using DataAccess;
using BusinessLogic.BusinessRules;
using BusinessLogic.ClassFactory;
using System.Xml;
using System.Text;

namespace BusinessLogic.BusinessRules
{
    /// <summary>
    /// WebSite 的摘要说明。
    /// </summary>
    public class WebSite : BaseClass
    {
        #region 必有属性
        public Model.Data.WebSiteData.WebSiteRow DataItem
        {
            get
            {
                return (Model.Data.WebSiteData.WebSiteRow)base.m_CurrentRow;
            }
            set
            {
                base.m_CurrentRow = value;
            }
        }

        private Model.Data.WebSiteData m_Data
        {
            get
            {
                return (Model.Data.WebSiteData)base.m_DataSet;
            }
            set
            {
                base.m_DataSet = value;
            }
        }


        public override int Count
        {
            get
            {
                return this.m_Data.WebSite.Count;
            }
        }
        #endregion

        #region 构造函数
        public WebSite(DataAccess.DBManager m_DBManager, DataAccess.BaseAccess m_Access)
            : base(m_DBManager, m_Access)
        {
        }
        #endregion

        #region 必须重载方法
        /// <summary>
        /// 根据数据集中索引,设置当前数据行
        /// </summary>
        /// <param name="n_index">数据集中的索引</param>
        public override void SelectRow(int n_index)
        {

            if (m_Data != null && m_Data.WebSite.Rows.Count > n_index)
            {
                DataItem = m_Data.WebSite[n_index];
            }
        }


        /// <summary>
        /// 实现更新
        /// </summary>
        /// <returns></returns>
        protected override int OnUpdate()
        {
            return m_Access.Update(this.m_DataSet);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        protected override bool OnDelete(int ID)
        {
            return true;
        }


        public override void NewDataSet()
        {

            this.m_Data = new WebSiteData();
            m_CurrentRow = m_Data.WebSite.NewWebSiteRow();
        }

        public override void SetDataSet(DataSet m_DataSet)
        {
            this.m_DataSet = m_DataSet;
        }

        public override bool OnInsert()
        {
            this.DataItem.intWebSiteID = this.GetNextSeed();
            this.m_Data.WebSite.Rows.Add(this.m_CurrentRow);
            return true;
        }
        #endregion

        #region 业务属性
        public Model.Data.WebSiteData Data
        {
            get
            {
                return (Model.Data.WebSiteData)base.m_DataSet;
            }
            set
            {
                base.m_DataSet = value;
            }
        }
        #endregion

        #region 业务函数
        public string GetWebSiteXml()
        {
            string[] anyFiles = new string[6] { "intWebSiteID", "chvKeyWords", "chvDescription", "chvAuthor" ,"chvTitle","chvWebUrl"};
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT ");
            for (int i = 0; i < anyFiles.Length; i++)
            {
                if (i == (anyFiles.Length - 1))
                {
                    strSql.Append(anyFiles[i]);
                }
                else
                {
                    strSql.Append(anyFiles[i] + ",");
                }
            }

            strSql.AppendFormat(" FROM WEBSITE WHERE INTWEBSITEID={0}", this.DataItem.intWebSiteID);

            DataTable tblTmp = this.m_Access.ExecuteDataTable(strSql.ToString());

            DataRow row = tblTmp.Rows[0];
            StringBuilder strXml = new StringBuilder();
            #region
            XmlTextWriter writer = new XmlTextWriter(new System.IO.StringWriter(strXml));
            try
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("TMPL");
                writer.WriteStartElement("WebSite");

                // 添加数据库字段值
                for (int i = 0; i < tblTmp.Columns.Count; i++)
                {
                    if (row[i] != DBNull.Value)
                    {
                        writer.WriteStartElement(tblTmp.Columns[i].ColumnName);
                        writer.WriteCData(row[i].ToString());
                        writer.WriteEndElement();
                    }
                }

                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteEndDocument();

                writer.Flush();
            }
            finally
            {
                writer.Close();
            }
            #endregion
            return strXml.ToString();
        }
        #endregion
    }
}
